﻿
using Prism.Ioc;
using Prism.Unity;
using System;
using System.Windows;
using ZDK.Infra.Services;
using ZDK.Utilities;
using ZDK.ViewModels;
using ZDK.Views;

namespace ZDK
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();

            //初始化无法用服务注入
            var pvd = (Current as PrismApplication).Container;
            var cfgSvr = pvd.Resolve<ICfgService>();
            if (!cfgSvr.Init())
            {
                MessageBox.Show("读取配置文件错误！");
                Environment.Exit(0); //退出程序
            }
        }

        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry reg)
        {
            //注册服务
            reg.RegisterSingleton<ICfgService, CfgService>(); //配置
            //reg.RegisterSingleton<IDialogService, DialogService>(); //对话框
            reg.RegisterSingleton<IMyLog, MyLog>(); //日志
            //reg.RegisterSingleton<Infrastructure.Services.IMyLog, Tools.MyLog>();

            //var container = PrismIocExtensions.GetContainer(containerRegistry);
            //container.AddNewExtension<Interception>()//注册服务和添加显示拦截
            //    .RegisterType<IMedicineSerivce, MedicineSerivce>(new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>())

            ////注册全局命令
            //containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();

            ////注册导航
            //containerRegistry.RegisterForNavigation<LoginMainContent>();

            ////注册对话框
            //reg.RegisterDialog<WarningMsgView, WarningMsgViewModel>("warning");
            reg.RegisterDialog<SettingsView, SettingsViewModel>("Settings");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            //解决MyServer.Close() 监听线程无法退出问题
            Environment.Exit(0);
            base.OnExit(e);
        }
    }
}
